package com.gs.SynPatrolRep.adapter.base;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public abstract class BaseAdapter<T> extends RecyclerView.Adapter<BaseViewHolder> {

    protected Context context;
    protected List<T> list;
    private int layoutId;
    protected boolean clickFlag = true;//单击事件和长单击事件的屏蔽标识

    public BaseAdapter(Context context, List<T> list, int layoutId) {
        this.context = context;
        this.list = list;
        this.layoutId = layoutId;
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        final BaseViewHolder holder = BaseViewHolder.get(context, parent, layoutId);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clickFlag) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClickListener(v, holder.getLayoutPosition());
                    }
                }
                clickFlag = true;
            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (onItemLongClickListener != null) {
                    onItemLongClickListener.onItemLongClickListener(v, holder.getLayoutPosition());
                }
                clickFlag = false;
                return false;
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
        convert(holder, list.get(position), position);
    }

    public abstract void convert(BaseViewHolder holder, T item, int position);

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    public void delete(int position) {
        list.remove(position);
        notifyItemRemoved(position);
        notifyItemRangeChanged(position, getItemCount() - position);
    }

    protected OnItemClickListener onItemClickListener;//单击事件

    public interface OnItemClickListener {
        void onItemClickListener(View v, int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    protected OnItemChildrenClickListener onItemChildrenClickListener;

    public interface OnItemChildrenClickListener {
        void OnItemChildrenClickListener(View v, int position);
    }

    public void setOnItemChildrenClickListener(OnItemChildrenClickListener onItemChildrenClickListener) {
        this.onItemChildrenClickListener = onItemChildrenClickListener;
    }

    protected OnItemLongClickListener onItemLongClickListener;

    public interface OnItemLongClickListener {
        void onItemLongClickListener(View v, int position);
    }

    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }
}
